package com.zretc.device.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zretc.dbtools.DBTools;
import com.zretc.device.entity.Device;

public class DeviceDao {
	/**
	 * 添加设备的方法
	 * 
	 * @return true，添加成功
	 */
	public boolean addDevice(Device device) {
		// 调用工具
		String sql = "insert into device values (?,?,?,?,?,?)";
		int n = DBTools.getUpdate(sql, device.getDevice_code(), device.getDevice_name(), device.getNow_num(),
				device.getHigh_num(), device.getLow_num(), device.getTotal_num());
		return n > 0;
	}

	/**
	 * 查询所有
	 * 
	 * @return 返回list
	 */
	public List<Device> findDeviceAll() {
		// 创建集合
		List<Device> list = new ArrayList<Device>();
		String sql = "select * from device";
		ResultSet rs = DBTools.getQuery(sql, null);
		// 迭代遍历
		try {
			while (rs.next()) {
				// 设备编号
				String device_code = rs.getString(1);
				// 设备名称
				String device_name = rs.getString(2);
				// 现有库存
				int now_num = rs.getInt(3);
				// 最大库存
				int high_num = rs.getInt(4);
				// 最小库存
				int low_num = rs.getInt(5);
				// 总数
				int total_num = rs.getInt(6);
				// 创建对象
				Device device = new Device(device_code, device_name, now_num, high_num, low_num, total_num);
				// 将对象添加到集合
				list.add(device);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
	
	/**
	 * 根据名称查询(模糊查询)
	 * 
	 * @return 返回list
	 */
	public List<Device> findDeviceByName(String devName) {
		// 创建集合
		List<Device> list = new ArrayList<Device>();
		String sql = "select * from device where device_name like ?";
		ResultSet rs = DBTools.getQuery(sql, "%"+devName+"%");
		// 迭代遍历
		try {
			while (rs.next()) {
				// 设备编号
				String device_code = rs.getString(1);
				// 设备名称
				String device_name = rs.getString(2);
				// 现有库存
				int now_num = rs.getInt(3);
				// 最大库存
				int high_num = rs.getInt(4);
				// 最小库存
				int low_num = rs.getInt(5);
				// 总数
				int total_num = rs.getInt(6);
				// 创建对象
				Device device = new Device(device_code, device_name, now_num, high_num, low_num, total_num);
				// 将对象添加到集合
				list.add(device);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
	/**
	 * 根据编号查询单个
	 * 
	 * @param code 设备编号
	 * @return
	 */
	public Device findDevByCo(String code) {
		Device device = null;
		String sql = "select * from device where device_code = ?";
		ResultSet rs = DBTools.getQuery(sql, code);
		try {
			if (rs.next()) {
				// 设备编号
				String device_code = rs.getString(1);
				// 设备名称
				String device_name = rs.getString(2);
				// 现有库存
				int now_num = rs.getInt(3);
				// 最大库存
				int high_num = rs.getInt(4);
				// 最小库存
				int low_num = rs.getInt(5);
				// 总数
				int total_num = rs.getInt(6);

				// 赋值
				device = new Device(device_code, device_name, now_num, high_num, low_num, total_num);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				rs.next();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return device;
	}

	/**
	 * 修改设备
	 * 
	 * @param device
	 * @return 是否修改成功
	 */
	public boolean updateDev(Device device) {
		//调用工具
		String sql = "update device set device_name=?,now_num=?,"
				+ "high_num=?,low_num=?,total_num=? where device_code=?";
		int n=DBTools.getUpdate(sql, device.getDevice_name(), device.getNow_num(), device.getHigh_num(), 
				device.getLow_num(),device.getTotal_num(), device.getDevice_code());
		return n>0;
	}
	
	/**
	 * 删除库存信息
	 * 
	 * @return
	 */
	public boolean delDev(String devCode) {
		String sql = "delete from device where device_code=?";
		int n = DBTools.getUpdate(sql, devCode);
		return n>0;
	}

}
